package com.yvon.service.demo.algorithm.offer;

import com.yvon.service.demo.algorithm.offer.ext.TreeNode;

/**
 * 二叉树的镜像
 * 请完成一个函数，输入一个二叉树，该函数输出它的镜像。
 * https://doocs.gitee.io/leetcode/#/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9827.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%95%9C%E5%83%8F/README
 */
public class P27 {

    public static void main(String[] args) {
        TreeNode nod1 = new TreeNode(1);
        TreeNode nod2 = new TreeNode(2);
        TreeNode nod3 = new TreeNode(32);
        TreeNode nod4 = new TreeNode(4);
        TreeNode nod5 = new TreeNode(5);
        TreeNode nod6 = new TreeNode(6);
        nod1.left = nod2;
        nod1.right = nod3;
        nod2.left = nod4;
        nod2.right = nod5;
        nod3.left = nod6;
        TreeNode.prePrint(nod1);
        Solution solution = new P27().new Solution();
        TreeNode treeNode = solution.mirrorTree(nod1);
        System.out.println();
        TreeNode.prePrint(treeNode);

    }

    class Solution {
        public TreeNode mirrorTree(TreeNode root) {
            if (root == null) {
                return null;
            }
            TreeNode k = root.right;
            root.right = root.left;
            root.left = k;
            mirrorTree(root.left);
            mirrorTree(root.right);
            return root;
        }
    }
}
